﻿using System;
using System.Net;
using System.Web.Mvc;
using RestUpMVC.Representation;
using RestUpMVC.Results;

namespace RestUpMVC
{
    public static class ControllerExtension
    {
        public static RestResult RestOk(this Controller controller, object resource)
        {
            string accept = controller.HttpContext.Request.Headers["accept"];
            var representation = RestUpMvcConfiguration.Instance.RepresentationFactory.GetRepresentationForContentType(accept, resource);
            return new RestOkResult(representation);
        }

        public static RestResult RestNoContent(this Controller controller)
        {
            return new RestNoContentResult();
        }

        public static RestResult RestCreated(this Controller controller, object resource)
        {
            string accept = controller.HttpContext.Request.Headers["accept"];
            var representation = RestUpMvcConfiguration.Instance.RepresentationFactory.GetRepresentationForContentType(accept, resource);
            return new RestCreatedResult(representation);
        }

        public static RestResult RestNotFound(this Controller controller, object resource)
        {
            string accept = controller.HttpContext.Request.Headers["accept"];
            var representation = RestUpMvcConfiguration.Instance.RepresentationFactory.GetRepresentationForContentType(accept, resource);
            return new RestNotFoundResult(representation);
        }

        public static RestResult RestResult(this Controller controller, HttpStatusCode statusCode, object resource)
        {
            string accept = controller.HttpContext.Request.Headers["accept"];
            var representation = RestUpMvcConfiguration.Instance.RepresentationFactory.GetRepresentationForContentType(accept, resource);
            return new RestResult(statusCode, representation);
        }
    }
}